using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Collections;

namespace WebForms.Demo_4
{
    public partial class RadGrid : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //This event is fired whenever the RadGrid needs more data. Examples of this is during 
        //initial page load (no data in the RadGrid), paging, sorting, filterting etc.
        protected void TelerikRadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            TelerikRadGrid.DataSource = SessionCustomerRepository.CustomerList();
        }

        //Occurs whenever a row in the RadGrid gets updated
        protected void TelerikRadGrid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            //Get the row being updated
            GridEditableItem currentItem = e.Item as GridEditableItem;
            String customerID = currentItem.GetDataKeyValue("CustomerID").ToString();

            //Find the customer we are updating
            EditableCustomer currentCustomer = SessionCustomerRepository.SelectOne(c => c.CustomerID == customerID);

            //UpdateValues(object) extracts all the values from the columns in the current row and
            //updates the values in our EditableCustomer object
            currentItem.UpdateValues(currentCustomer);

        }

        //Occurs whenever a new row is inserted into the RadGrid
        protected void TelerikRadGrid_InsertCommand(object sender, GridCommandEventArgs e)
        {
            //Get row being inserted
            GridEditableItem currentItem = e.Item as GridEditableItem;
            EditableCustomer currentCustomer = new EditableCustomer();

            //UpdateValues(object) extracts all the values from the columns in the current row and
            //updates the values in our EditableCustomer object
            currentItem.UpdateValues(currentCustomer);

            //insert the customer in our list
            SessionCustomerRepository.Insert(currentCustomer);
        }

        //Occurs whenever a row is deleted from the RadGrid
        protected void TelerikRadGrid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            //Get the row being deleted
            GridEditableItem currentItem = e.Item as GridEditableItem;
            String customerID = currentItem.GetDataKeyValue("CustomerID").ToString();

            //Find the customer we are attempting to delete
            EditableCustomer currentCustomer = SessionCustomerRepository.SelectOne(c => c.CustomerID == customerID);

            //Delete the customer from our list
            SessionCustomerRepository.Delete(currentCustomer);
        }
    }
}